using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using DTO;

namespace DAOLayer
{
    public class TinhTrangDao
    {
        public static DataTable LayDanhSachTinhTrang()
        {
            DataTable dt = new DataTable();
            // B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
            OleDbConnection cn = Database.ConnectionData();
            //'B3: Tao chuoi strSQL thao tac CSDL
            String strSQL = "Select * From TINHTRANGPHONG";
            //'B4: Thuc thi chuoi strSQL
            OleDbDataAdapter da = new OleDbDataAdapter(strSQL, cn);
            da.Fill(dt);
            // 'B5: Dong ket noi CSDL
            cn.Close();
            return dt;

        }
        public static TinhTrangDto LayTinhTrang(int ma)
        {
            TinhTrangDto ttrang = new TinhTrangDto();
            // B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
            OleDbConnection cn = Database.ConnectionData();
            //'B3: Tao chuoi strSQL thao tac CSDL
            String strSQL = "Select * From TINHTRANGPHONG Where MaTinhTrang = ? ";
            //'B4: Thuc thi chuoi strSQL
            OleDbCommand cmd = new OleDbCommand(strSQL, cn);
            cmd.Parameters.Add("@MaTinhTrang", OleDbType.Integer);
            cmd.Parameters[("@MaTinhTrang")].Value = ma;
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                ttrang.MaTinhTrang = (int)dr["MaTinhTrang"];
                ttrang.TenTinhTrang = (string)dr["TenTinhTrang"];

            }
            cn.Close();
            return ttrang;

        }
        public static TinhTrangDto LayTinhTrangTheoTen(string ten)
        {
            TinhTrangDto ttrang = new TinhTrangDto();
            // B1 & B2: Tao chuoi ket noi, mo ket noi bang doi tuong ket noi
            OleDbConnection cn = Database.ConnectionData();
            //'B3: Tao chuoi strSQL thao tac CSDL
            String strSQL = "Select * From TINHTRANGPHONG Where TenTinhTrang = ? ";
            //'B4: Thuc thi chuoi strSQL
            OleDbCommand cmd = new OleDbCommand(strSQL, cn);
            cmd.Parameters.Add("@TenTinhTrang", OleDbType.WChar);
            cmd.Parameters[("@TenTinhTrang")].Value = ten;
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                ttrang.MaTinhTrang = (int)dr["MaTinhTrang"];
                ttrang.TenTinhTrang = (string)dr["TenTinhTrang"];

            }
            cn.Close();
            return ttrang;

        }
        public static void CapNhatXuongCSDL(TinhTrangDto tt)
        {
            OleDbConnection cn = Database.ConnectionData();
            //'B3: Tao chuoi strSQL thao tac CSDL
            String strSQL = "Update TINHTRANGPHONG Set TenTinhTrang = ? Where MaTinhTrang = ?  ";
            OleDbCommand cmd = new OleDbCommand(strSQL, cn);
            cmd.Parameters.Add("@TenTinhTrang", OleDbType.WChar);
            cmd.Parameters["@TenTinhTrang"].Value = tt.TenTinhTrang;
            cmd.Parameters.Add("@MaTinhTrang", OleDbType.Integer);
            cmd.Parameters[("@MaTinhTrang")].Value = tt.MaTinhTrang;
            cmd.ExecuteNonQuery();
            cn.Close();
        }
    }
}
